package com.helen_dragon.healthsystem.controller;/*
 *@Author: helen
 *@Date:   2021/6/7 21:28
 *@Description:
 */

import com.helen_dragon.healthsystem.config.TokenProcessor;
import com.helen_dragon.healthsystem.model.information;
import com.helen_dragon.healthsystem.model.person;
import com.helen_dragon.healthsystem.service.personService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/userPage")
public class userController {

    @Autowired
    private personService perService;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }



    @RequestMapping("/login")
    public String login(@ModelAttribute person per, Model model){
        person person2 = perService.selectPerson(per.getPersonId());
        String personId = per.getPersonId();
        String password = per.getPassword();
        if(person2 != null){
            if(per.getPassword().equals(person2.getPassword())){
                return "redirect:"+ personId + "/inputInfo";
            }
            else {
                String str= "密码错误，请重新输入！";
                model.addAttribute("personId", personId);
                model.addAttribute("password", password);
                model.addAttribute("passwordError", str);
                return "index";
            }
        }
        else {
            String str= "该账号尚未注册，请先注册";
            model.addAttribute("personId", personId);
            model.addAttribute("password", password);
            model.addAttribute("passwordError", str);
            model.addAttribute("userError", str);
            return "index";
        }
    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @RequestMapping("/insertPerson")
    public String insertPerson(@ModelAttribute person per, Model model){
        int flag = perService.insertPerson(per);
        if (flag == 1){
            String name = per.getName();
            model.addAttribute("name", name);
            return "registerSuccess";
        }
        else {
            String name = per.getName();
            String personId = per.getPersonId();
            String password = per.getPassword();
            model.addAttribute("name", name);
            model.addAttribute("personId", personId);
            model.addAttribute("password", password);
            model.addAttribute("userExists","该用户已存在");
            return "register";
        }
    }

    @RequestMapping("/{personId}/inputInfo")
    public String inputInfo(@PathVariable("personId") String personId,
                            HttpServletRequest request, Model model){
        person person = perService.selectPerson(personId);
        boolean flag = perService.is_clock_in(personId);
        if(flag == true)
            return "redirect:/userPage/insertInformation/" + personId;

        model.addAttribute("name",person.getName());
        model.addAttribute("personId",person.getPersonId());
        model.addAttribute("information", new information());
        model.addAttribute("flag",0);

        String token = TokenProcessor.getInstance().makeToken();//创建令牌
        request.getSession().setAttribute("token", token);  //在服务器使用session保存token(令牌)

        return "inputInformation";
    }

    private boolean isRepeatSubmit(HttpServletRequest request) {
        String client_token = request.getParameter("token");
        //1、如果用户提交的表单数据中没有token，则用户是重复提交了表单
        if(client_token==null){
            return true;
        }
        //取出存储在Session中的token
        String server_token = (String) request.getSession().getAttribute("token");
        //2、如果当前用户的Session中不存在Token(令牌)，则用户是重复提交了表单
        if(server_token==null){
            return true;
        }
        //3、存储在Session中的Token(令牌)与表单提交的Token(令牌)不同，则用户是重复提交了表单
        if(!client_token.equals(server_token)){
            return true;
        }
        return false;
    }

    @RequestMapping("/insertInformation/{personId}")
    public String insertInformation(@ModelAttribute information info,
                                    @PathVariable String personId,
                                    HttpServletRequest request,
                                    Model model){

        boolean b = isRepeatSubmit(request);//判断用户是否是重复提交
        if(b==true){
            System.out.println("请不要重复提交");
        }else {
            request.getSession().removeAttribute("token");//移除session中的token
            System.out.println("处理用户提交请求！！");
            perService.InsertInfo(info, personId);
        }
        model.addAttribute("information", info);
        person person = perService.selectPerson(personId);
        model.addAttribute("name", person.getName());
        model.addAttribute("personId", person.getPersonId());
        model.addAttribute("message", "您今天已打卡成功！");
        model.addAttribute("flag", 1);
        return "inputInformation";
    }


}
